(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.globSystemConfig.updateVerifyController', editController);
	editController.$inject = [ '$scope', '$timeout', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'systemConfigService' ];
	function editController($scope, $timeout, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, systemConfigService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.globSystemConfig = {};
		ctrl.globSystemConfigOld = {};

		ctrl.init = function() {
			systemConfigService.initGlobDynaParm($scope, ctrl, gridService);
			systemConfigService.getGlobServiceCode(ctrl).then(
					function(serviceCodeOptions){
						authService.updateVerify(params).then(function(response) {
							if (response.data && response.data.model && response.data.task) {
								ctrl.globSystemConfig  = response.data.model.newModel || {};
								ctrl.globSystemConfigOld = response.data.model.oldModel || {};
								ctrl.globSystemConfig.serviceCode = serviceCodeOptions[0].value;
								ctrl.globSystemConfigOld.serviceCode = serviceCodeOptions[0].value;
								ctrl.globSystemConfig.taskId = response.data.task.taskId;
								ctrl.globSystemConfig.dynamicParams = ctrl.globSystemConfig.dynamicParams || {};
								ctrl.isInitialized = "Y";
							}
						});
					}
			);
		};
		
		 ctrl.getGlobDynaparm = function(){
			 if(ctrl.isInitialized == "Y"){
		         systemConfigService.getGlobDynaparmTwo(ctrl,ctrl.globSystemConfig);
		     }else {
		         ctrl.isInitialized = "N";
		     }
	     };
			
		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.globSystemConfig.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						angular.forEach(ctrl.globSystemConfig.data, function(serviceEntity) {
							angular.forEach(serviceEntity, function(entity, index) {
								if (entity.dynamicParams[entity.name]) {
									ctrl.globSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
								}
							});
						});
						authService.updateVerifyApprove(ctrl.globSystemConfig).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.globSystemConfig.comments = comments;
						angular.forEach(ctrl.globSystemConfig.data, function(serviceEntity) {
							angular.forEach(serviceEntity, function(entity, index) {
								if (entity.dynamicParams[entity.name]) {
									ctrl.globSystemConfig.dynamicParams[entity.dynamicParameter.serviceCode + '_' + entity.name] = angular.merge(entity.dynamicParams[entity.name], entity.dynamicParameter);
								}
							});
						});
						authService.updateVerifyReject(ctrl.globSystemConfig).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				},{
					'name' : 'title.infobar.globSystemConfig.update',
					'state' : 'cda.globSystemConfig.update',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				},{
					'name' : 'title.infobar.globSystemConfig.update',
					'state' : 'cda.globSystemConfig.update',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
